<?php

namespace app\common\model;

use think\facade\Db;
use think\Model;

/**
 * Findstore
 */
class Findstore extends Model
{
    // 表名
    protected $name = 'findstore';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected $append = [
        'rent_unit_text',
        'city_text',
        'industry_text'
    ];

    public function getIndustryTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['industry'])->column('string');
    }

    //某某人找某地区某大小铺面做某行业
    public function setNameAttr($value, $data)
    {
        $contact_person_str = isset($data['contact_person']) ? $data['contact_person'] : '';
        $acreage = isset($data['start_area']) ? $data['start_area'] . '㎡' : '0';
        $str2 = '';
        $city = isset($data['city']) ? $data['city'] : '';
        if ($city) {
            $query_name = Db::name('area')
                ->whereIn('id', $city)
                ->cache(true)
                ->column('id,name,pid');
            $names_ar=array_column($query_name, 'name');
            $ar_all=Db::name('area')->where('pid',$query_name[0]['pid'])->count('id');
            if(count($names_ar)==$ar_all){
                $city_name='全'.Db::name('area')->where('id',$query_name[0]['pid'])->cache(true)->value('name');
            }else{
                $city_name=implode(',',$names_ar);
            }
        }
        $str2 = $contact_person_str . '找' . $city_name . $acreage . '做';
        $industry_ids = isset($data['industry']) ? $data['industry'] : '';
        $industry_names = Db::name('tags')->whereIn('id', $industry_ids)->order('weigh', 'desc')->limit(3)
            ->column('string');
        $str2 .= implode(',', $industry_names);
        return $str2;
//        $area=
//        $qu_name=Db::name('area')->where(['id'=>])->cache(true)->value('name');
    }

    public function getCityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function getAddressAttr($value, $row)
    {
        if ($row['city'] === '' || $row['city'] === null) return '';

        $query_name = Db::name('area')
            ->whereIn('id', $row['city'])
            ->cache(true)
            ->column('id,name,pid');
        $names_ar=array_column($query_name, 'name');
        $ar_all=Db::name('area')->where('pid',$query_name[0]['pid'])->count('id');
        if(count($names_ar)==$ar_all){
            $city_name='全'.Db::name('area')->where('id',$query_name[0]['pid'])->cache(true)->value('name');
        }else{
            $city_name=implode(',',$names_ar);
        }
        return $city_name;
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->where('level', '3')->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }

    public function setCityAttr($value): string
    {

        if ($value) $this->set('seach_area', is_array($value) ? $value[count($value) - 1] : $value);
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $query_name = Db::name('area')
            ->whereIn('id', $row['city'])
            ->cache(true)
            ->column('id,name,pid');
        $names_ar=array_column($query_name, 'name');
        $ar_all=0;
        if($query_name){
            $ar_all=Db::name('area')->where('pid',$query_name[0]['pid'])->count('id');
        }
        if(count($names_ar)==$ar_all){
            $city_name='全'.Db::name('area')->where('id',$query_name[0]['pid'])->cache(true)->value('name');
        }else{
            $city_name=implode(',',$names_ar);
        }
        return $city_name;
    }

    public function getRentUnitList()
    {
        //租金单位:0=元/月,1=元/㎡/月
        return ['0' => __('元/月'), '1' => __('元/㎡/月'), 2 => '元/年'];
    }

    public function getRentUnitTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rent_unit']) ? $data['rent_unit'] : '');
        $list = $this->getRentUnitList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function setUpdateTimeAttr($value, $data)
    {
        if (!is_numeric($value)) {
            return strtotime($value);
        }
        return $value;
    }

//    public function setSeachAreaAttr($value, $data)
//    {
//        $last_city = isset($data['city']) ? $data['city'] : '';
//        if ($last_city) {
//            if (is_array($last_city)) {
//                return $last_city[count($last_city) - 1];
//            } else {
//                $lsts_city = explode(',', $last_city);
//                return $lsts_city[count($last_city) - 1];
//            }
//        }
//        return '12';
//    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
    public function uinfo(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id')->bind(['mobile','avatar','nickname','domain_avatar']);
    }


    public function setIndustryAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getIndustryAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }
}
